﻿using HANS.WeChat.Impl;
using Microsoft.AspNetCore.Mvc;
using Senparc.Weixin;
using System.Threading.Tasks;

namespace WeChatSite.Area.WeChat
{
    [Area("WeChat")]
    public class SmallProgramLoginController : Controller
    {

        SmallProgramLogin wxOpenLogin = new SmallProgramLogin();
        public IActionResult Index()
        {

            return View();
        }

        /// <summary>
        /// 登录凭证校验
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public Task<IActionResult> Login(string code)
        {

            return Task.Run<IActionResult>(() =>
            {
                return Json(wxOpenLogin.GetThirdSessionId(Config.DefaultSenparcWeixinSetting.WxOpenAppId, Config.DefaultSenparcWeixinSetting.WxOpenAppSecret, code));
            });

        }

        /// <summary>
        /// 检查登录是否过期
        /// </summary>
        /// <param name="thirdSessionId"></param>
        /// <returns></returns>
        public Task<IActionResult> CheckLoginToken(string thirdSessionId)
        {
            return Task.Run<IActionResult>(() =>
            {
                return Json(wxOpenLogin.CheckLoginToken(thirdSessionId));
            });

        }

        /// <summary>
        /// 注册用户信息
        /// </summary>
        /// <param name="iv"></param>
        /// <param name="encryptedData"></param>
        /// <param name="signature"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public Task<IActionResult> RegisterUser(string iv, string encryptedData, string signature, string token)
        {
            return Task.Run<IActionResult>(() =>
            {
                return Json(wxOpenLogin.RegisterUser(iv,encryptedData,token,Config.DefaultSenparcWeixinSetting.WxOpenAppId));
            });
        }
    }
}